package com.tanhua.admin.module.dynamicmanage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.admin.module.dynamicmanage.pojo.Totals;
import com.tanhua.admin.module.dynamicmanage.service.PublishInfoService;
import com.tanhua.common.mapper.PublishInfoMapper;
import com.tanhua.common.pojo.PublishInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PublishInfoServiceImpl implements PublishInfoService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    @Override
    public Map<String,Object> queryPublishInfoList(Integer page, Integer pagesize, Object sortProp, Object sortOrder,
                                                   Object userId, Object sd, Object ed, Object uid, Object state) {


        //查询圈子信息表
        QueryWrapper<PublishInfo> queryWapperPI = new QueryWrapper<>();

        //设置根据喜欢数量,点赞数量,转发数量查询
        if(sortOrder.equals("ascending")){
            queryWapperPI.orderByAsc(StrUtil.toUnderlineCase(sortProp.toString()));
        }else if (sortOrder.equals("descending")){
            queryWapperPI.orderByDesc(StrUtil.toUnderlineCase(sortProp.toString()));
        }

        //如果消息id不为空,将消息id放入查询语句中
        if (ObjectUtil.isNotEmpty(userId)) {
            queryWapperPI.like("user_id", userId);
        }
        //如果起止时间不为零,进行时间搜索

        if (ObjectUtil.isAllNotEmpty(sd, ed) && Long.valueOf(sd.toString()) >= 0 && Long.valueOf(ed.toString()) >= 0) {
            queryWapperPI.ge("create_date", sd).le("create_date", ed);
        }

        //判断状态是否为空
        if (ObjectUtil.isNotEmpty(state)) {
            //判断集合是否为all
            if (state.equals("all")) {

                queryWapperPI.in("state", "3","4","5");
            } else {
                queryWapperPI.eq("state", state);
            }
        }

        if(ObjectUtil.isNotEmpty(uid)){
            queryWapperPI.eq("user_id", uid);
        }


        //得到分页对象
        IPage<PublishInfo> publishInfoIPage = publishInfoMapper.selectPage(new Page<PublishInfo>(page, pagesize), queryWapperPI);

        HashMap<String, Object> map = new HashMap<>();
        map.put("publishInfoIPage",publishInfoIPage);
        Integer count = publishInfoMapper.selectCount(queryWapperPI);
        map.put("count",count);
        return map;
    }

    @Override
    public List<Totals> queryStateList(Object userId,Object sd,Object ed) {

        //全部
        QueryWrapper<PublishInfo> queryWrapperCount4All = new QueryWrapper<>();
        //待审核
        QueryWrapper<PublishInfo> queryWrapperCount43 = new QueryWrapper<>();
        //已通过
        QueryWrapper<PublishInfo> queryWrapperCount44 = new QueryWrapper<>();
        //已驳回
        QueryWrapper<PublishInfo> queryWrapperCount45 = new QueryWrapper<>();
        //如果userId不为零
        if(ObjectUtil.isNotEmpty(userId)){
            queryWrapperCount4All.like("user_id",userId);
            queryWrapperCount43.like("user_id",userId);
            queryWrapperCount44.like("user_id",userId);
            queryWrapperCount45.like("user_id",userId);
        }

        if(ObjectUtil.isAllNotEmpty(sd,ed)){
            Long sdL = Long.valueOf(sd.toString());
            Long edL = Long.valueOf(ed.toString());
            if(sdL>=0&&edL>=0){
                queryWrapperCount4All.ge("create_date", sd).le("create_date", ed);
                queryWrapperCount43.ge("create_date", sd).le("create_date", ed);
                queryWrapperCount44.ge("create_date", sd).le("create_date", ed);
                queryWrapperCount45.ge("create_date", sd).le("create_date", ed);
            }
        }
        List<Totals> totalList = new ArrayList<>();

        //查询全部状态 totals
        queryWrapperCount4All.in("state", 3, 4, 5);
        //全部状态的个数
        Integer countAll = publishInfoMapper.selectCount(queryWrapperCount4All);
        Totals totals1 = new Totals("全部","all",countAll);


        //查询状态为待审核的totals
        queryWrapperCount43.eq("state", 3);
        //状态为3的总个数
        Integer count3 = publishInfoMapper.selectCount(queryWrapperCount43);
        Totals totals2 = new Totals("待审核","3",count3);


        //查询状态为已驳回的totals
        queryWrapperCount44.eq("state", 4);
        //状态为4的总个数
        Integer count4 = publishInfoMapper.selectCount(queryWrapperCount44);
        Totals totals3 = new Totals("已驳回","4",count4);


        //查询状态为已通过的totals
        queryWrapperCount45.eq("state", 5);
        //状态为5的总个数
        Integer count5 = publishInfoMapper.selectCount(queryWrapperCount45);
        Totals totals4 = new Totals("已通过","5",count5);


        //将全部状态的map放入状态集合中
        totalList.add(totals1);
        totalList.add(totals2);
        totalList.add(totals3);
        totalList.add(totals4);

        return totalList;
    }

    @Override
    public Map<String, String> undoAudit(String[] id) {
        Map<String, String> result = new HashMap<>();
        if(ObjectUtil.isNotEmpty(id)){
            UpdateWrapper<PublishInfo> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("publish_id",id[0]).set("state",3);
            int update = publishInfoMapper.update(null, updateWrapper);
            if(update==1){
                result.put("messgae","操作成功");
                return result;
            }
        }
        result.put("messgae","操作失败");
        return result;
    }
}
